package lc.com.lcpicturebackend.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lc.com.lcpicturebackend.common.BaseResponse;
import lc.com.lcpicturebackend.common.ResultUtils;
import lc.com.lcpicturebackend.exception.BusinessException;
import lc.com.lcpicturebackend.exception.ErrorCode;
import lc.com.lcpicturebackend.exception.ThrowUtils;
import lc.com.lcpicturebackend.model.dto.chatai.ConversationEditeRequest;
import lc.com.lcpicturebackend.model.dto.chatai.ConversationQueryRequest;
import lc.com.lcpicturebackend.model.entity.Conversation;
import lc.com.lcpicturebackend.model.entity.User;
import lc.com.lcpicturebackend.service.ConversationService;
import lc.com.lcpicturebackend.service.UserService;
import org.springframework.web.bind.annotation.*;


/**
 * 会话控制器
 */
@RestController
@CrossOrigin
@RequestMapping("/conversation")
public class ConversationController {

    @Resource
    private ConversationService conversationService;

    @Resource
    private UserService userService;


    /**
     * 获取聊天会话信息
     *
     * @param conversationId 会话id
     * @return 会话信息
     */
    @PostMapping("/getConversation")
    public BaseResponse<Conversation> getConversation(String conversationId, HttpServletRequest request) {
        ThrowUtils.throwIf(StrUtil.isBlank(conversationId) || conversationId.length() < 10, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        Conversation result = conversationService.lambdaQuery()
                .eq(Conversation::getUserId, userId)
                .eq(Conversation::getConversationId, conversationId)
                .one();
        if (ObjectUtil.isEmpty(result)) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "获取会话失败");
        }
        return ResultUtils.success(result);
    }

    /**
     * 获取聊天会话列表
     *
     * @param conversationQueryRequest 会话id
     * @return 会话
     */
    @PostMapping("/getConversationBypage")
    public BaseResponse<Page<Conversation>> getConversationByPage(@RequestBody ConversationQueryRequest conversationQueryRequest,
    HttpServletRequest request) {
        ThrowUtils.throwIf(ObjectUtil.isEmpty(conversationQueryRequest), ErrorCode.PARAMS_ERROR);
        long current = conversationQueryRequest.getCurrent();
        long pageSize = conversationQueryRequest.getPageSize();
        User loginUser = userService.getLoginUser(request);

        Page<Conversation> conversationPage = conversationService.page(new Page<>(current, pageSize),
                conversationService.getQueryWrapper(conversationQueryRequest,loginUser.getId()));

        return ResultUtils.success(conversationPage);
    }


    /**
     * 删除图片
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteConversation( String conversationId, HttpServletRequest request) {
        if (conversationId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NO_AUTH_ERROR);
        conversationService.deleteConversation(conversationId, loginUser);
        return ResultUtils.success(true);
    }


    /**
     * 编辑会话标题
     */
    @PostMapping("/edit")
    public BaseResponse<Boolean> editConversationTitle(@RequestBody ConversationEditeRequest conversationEditeRequest,
                                                       HttpServletRequest request) {
        ThrowUtils.throwIf(ObjectUtil.isEmpty(conversationEditeRequest), ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();

        boolean update = conversationService.lambdaUpdate()
                .eq(Conversation::getConversationId, conversationEditeRequest.getConversationId())
                .eq(Conversation::getUserId, userId)
                .set(Conversation::getTitle, conversationEditeRequest.getTitle())
                .update();
        if (!update) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "会话不存在");
        }
        return ResultUtils.success(true);
    }


}



